package com.sqx.modules.indentTimeRecord.controller;

import com.sqx.common.utils.Result;
import com.sqx.modules.app.annotation.Login;
import com.sqx.modules.indentTimeRecord.entity.IndentTimeRecord;
import com.sqx.modules.indentTimeRecord.service.IndentTimeRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 加时记录-用户端
 * @author wuChang
 * @since 2024-05-22
 */
@Slf4j
@RestController
@Api(value = "加时记录-用户端", tags = {"加时记录-用户端"})
@RequestMapping("/app/indentTimeRecord/")
public class AppIndentTimeRecordController {
    @Autowired
    private IndentTimeRecordService indentTimeRecordService;

    private final ReentrantReadWriteLock reentrantReadWriteLock = new ReentrantReadWriteLock(true);
    /**
     * 获取加时记录列表
     * @param indentTimeRecord 加时记录
     */
    @ApiOperation("获取加时记录列表")
    @GetMapping(value = "getIndentTimeRecordList")
    public Result getIndentTimeRecordList(Integer page, Integer limit, IndentTimeRecord indentTimeRecord) {

        return Result.success().put("data", indentTimeRecordService.getIndentTimeRecordList(page, limit, indentTimeRecord));
    }

    @Login
    @ApiOperation("订单加时")
    @PostMapping(value = "indentAddTime")
    public Result indentAddTime(@RequestAttribute Long userId,Long indentId,Long timeId){
        reentrantReadWriteLock.writeLock().lock();
        try {
            return indentTimeRecordService.indentAddTime(userId,indentId,timeId);
        } catch (Exception e) {
            log.error("加时异常：" + e.getMessage(), e);
        } finally {
            reentrantReadWriteLock.writeLock().unlock();
        }
        return Result.error("系统繁忙，请稍后再试！");
    }

}